package viettravel.guiandevent.map;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import viettravel.common.Constants;
import viettravel.guiandevent.R;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;

public class GooglemapV2GetLatLngActivity extends SherlockFragmentActivity {
	private Double mLat;
	private Double mLng;
	private GoogleMap map;
	private String mPlaceName;
	private boolean flag = true;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTheme(Constants.THEME);
		setContentView(R.layout.gmap_api_v2);
		ActionBar actionBar = getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		actionBar.setDisplayHomeAsUpEnabled(false);
		actionBar.setHomeButtonEnabled(true);
		actionBar.setIcon(R.drawable.ic_back_arrow);
		actionBar.setBackgroundDrawable(Constants.APP_DRAWABLE);
		actionBar.setTitle(getString(R.string.newplace_getlatlng_title));
		SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.googleMapAPIMap);

		map = fm.getMap();
		map.setMyLocationEnabled(true);
		map.setOnMyLocationChangeListener(new OnMyLocationChangeListener() {
			@Override
			public void onMyLocationChange(Location location) {
				// TODO Auto-generated method stub
				if (flag) {
					mLat = location.getLatitude();
					mLng = location.getLongitude();
					map.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(
							mLat, mLng)));
					map.animateCamera(CameraUpdateFactory.zoomTo(14), 3000,
							null);
					flag = false;
				}
			}
		});

		map.setOnMapLongClickListener(new OnMapLongClickListener() {
			@Override
			public void onMapLongClick(LatLng latlng) {
				map.clear();
				GooglemapCommon.addMarker(map, latlng,
						R.drawable.ic_my_position);
				mLat = latlng.latitude;
				mLng = latlng.longitude;
			}
		});
		try {
			Bundle extra = getIntent().getExtras();
			mPlaceName = extra
					.getString(Constants.EXTRA_NEWPLACE_CHECKNAME_PLACENAME);
		} catch (Exception e) {
			Log.v(Constants.TAG, "loi");
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home: {
			finish();
			return true;
		}
		case R.id.newplace_getlatlng_done: {
			Log.v(Constants.TAG, "Come on");
			Log.v(Constants.TAG + "getlatlng", mPlaceName);
			Log.v(Constants.TAG + "getlatlng", mLat + "");
			Log.v(Constants.TAG + "getlatlng", mLng + "");
			Intent i = new Intent();
			i.putExtra(Constants.EXTRA_PLACE_NAME, mPlaceName);
			i.putExtra(Constants.EXTRA_PLACE_LONG, mLng);
			i.putExtra(Constants.EXTRA_PLACE_LAT, mLat);
			setResult(RESULT_OK, i);
			finish();
		}
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu _menu) {
		_menu.add(Menu.NONE, R.id.newplace_getlatlng_done, Menu.NONE,
				this.getString(R.string.newplace_getlatlng_done))
				.setIcon(R.drawable.abs__ic_cab_done_holo_light)
				.setShowAsAction(
						MenuItem.SHOW_AS_ACTION_IF_ROOM
								| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
		return super.onCreateOptionsMenu(_menu);
	}
}
